<template>
  <div class="login-container">
    <div class="login-box">
      <div class="background-image">
        <!-- 背景图片，根据需要替换 -->
        <img src="https://via.placeholder.com/300x400/6495ED/FFFFFF?text=西湖分信用" alt="西湖分信用" />
      </div>
      <div class="login-form-wrapper">
        <el-tabs v-model="activeTab" class="login-tabs">
          <el-tab-pane label="登录" name="login">
            <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef" class="login-form">
              <el-form-item prop="phone">
                <el-input v-model="loginForm.phone" placeholder="请输入手机号" prefix-icon="el-icon-user"></el-input>
              </el-form-item>
              <el-form-item prop="password" v-if="!showVerificationCodeLogin">
                <el-input type="password" v-model="loginForm.password" placeholder="请输入密码" prefix-icon="el-icon-lock" show-password></el-input>
              </el-form-item>
              <el-form-item prop="verificationCode" v-if="showVerificationCodeLogin">
                <el-input v-model="loginForm.verificationCode" placeholder="请输入验证码" prefix-icon="el-icon-message">
                  <template #append>
                    <el-button @click="getVerificationCode" :disabled="countdown > 0">
                      {{ countdown > 0 ? `${countdown}s 后重发` : '获取验证码' }}
                    </el-button>
                  </template>
                </el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" class="login-button" @click="handleLogin">立即登录</el-button>
              </el-form-item>
              <div class="login-options">
                <el-link type="primary" @click="toggleLoginMethod">{{ showVerificationCodeLogin ? '密码登录' : '验证码登录' }}</el-link>
              </div>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="注册" name="register">
            <el-form :model="registerForm" :rules="registerRules" ref="registerFormRef" class="register-form">
              <el-form-item prop="username">
                <el-input v-model="registerForm.username" placeholder="请输入用户名" prefix-icon="el-icon-user"></el-input>
              </el-form-item>
              <el-form-item prop="phone">
                <el-input v-model="registerForm.phone" placeholder="请输入手机号" prefix-icon="el-icon-mobile-phone"></el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input type="password" v-model="registerForm.password" placeholder="请输入密码" prefix-icon="el-icon-lock" show-password></el-input>
              </el-form-item>
              <el-form-item prop="confirmPassword">
                <el-input type="password" v-model="registerForm.confirmPassword" placeholder="请确认密码" prefix-icon="el-icon-lock" show-password></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" class="login-button" @click="handleRegister">立即注册</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
// 假设这里引入 API 调用
// import { login, register, sendVerificationCode } from '@/api/auth'

const activeTab = ref('login')
const loginFormRef = ref(null)
const registerFormRef = ref(null)
const showVerificationCodeLogin = ref(false)
const countdown = ref(0)
let timer = null

const loginForm = reactive({
  phone: '',
  password: '',
  verificationCode: ''
})

const registerForm = reactive({
  username: '',
  phone: '',
  password: '',
  confirmPassword: ''
})

const loginRules = reactive({
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码至少包含6位', trigger: 'blur' }
  ],
  verificationCode: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { len: 6, message: '验证码为6位', trigger: 'blur' }
  ]
})

const registerRules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 10, message: '用户名长度在3到10个字符', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码至少包含6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== registerForm.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
})

const toggleLoginMethod = () => {
  showVerificationCodeLogin.value = !showVerificationCodeLogin.value
  // 重置表单验证
  loginFormRef.value.resetFields()
}

const getVerificationCode = async () => {
  // 模拟人机验证
  try {
    await ElMessageBox.confirm('请完成人机验证', '验证', {
      confirmButtonText: '完成',
      cancelButtonText: '取消',
      type: 'info'
    })
    
    // 假设人机验证通过，发送验证码
    // await sendVerificationCode(loginForm.phone)
    ElMessage.success('验证码已发送，请注意查收')
    countdown.value = 60
    timer = setInterval(() => {
      if (countdown.value > 0) {
        countdown.value--
      } else {
        clearInterval(timer)
        timer = null
      }
    }, 1000)
  } catch (error) {
    ElMessage.info('人机验证未通过或已取消')
  }
}

const handleLogin = () => {
  loginFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        // 根据登录方式调用不同API
        if (showVerificationCodeLogin.value) {
          // await loginByVerificationCode(loginForm.phone, loginForm.verificationCode)
          ElMessage.success('验证码登录成功！')
          $router.push('/home')
        } else {
          // await login(loginForm.phone, loginForm.password)
          ElMessage.success('密码登录成功！')
          $router.push('/home')

        }
        // 登录成功后跳转到首页或其他页面
        // router.push('/')
      } catch (error) {
        ElMessage.error('登录失败，请检查手机号或密码')
      }
    } else {
      ElMessage.error('请填写完整登录信息')
    }
  })
}

const handleRegister = () => {
  registerFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        // await register(registerForm)
        ElMessage.success('注册成功！')
        // 注册成功后跳转到完善信息页面
        //router.push('/register-info')
        $router.push('/register-info')
        
        //$router.push('/register-info')
        //activeTab.value = 'login' // 注册成功后切换回登录页面
      } catch (error) {
        ElMessage.error('注册失败，请稍后重试')
      }
    } else {
      ElMessage.error('请填写完整注册信息')
    }
  })
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: url('https://via.placeholder.com/1920x1080/add8e6/FFFFFF?text=Background') no-repeat center center;
  background-size: cover;
  position: relative;
}

.login-box {
  display: flex;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  width: 800px;
  max-width: 90%;
  height: 500px; /* 根据图片调整高度 */
}

.background-image {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #6495ED; /* 匹配图片左侧蓝色调 */
}

.background-image img {
  max-width: 80%;
  height: auto;
}

.login-form-wrapper {
  flex: 1;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.login-tabs .el-tab-pane {
  padding-top: 20px;
}

.login-form, .register-form {
  width: 100%;
}

.login-button {
  width: 100%;
  margin-top: 20px;
}

.login-options {
  text-align: right;
  margin-top: 10px;
}

/* 覆盖Element Plus的默认样式 */
.login-tabs .el-tabs__item {
  font-size: 16px;
  font-weight: bold;
}
</style> 